package com.pig4cloud.pig.module.controller.wx;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wx.UserAddress;
import com.pig4cloud.pig.module.service.wx.IUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户地址Controller
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@Validated
@Api(value = "用户地址控制器", tags = {"用户地址管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/user/address")
public class UserAddressController {

    private final IUserAddressService userAddressService;

    /**
     * 查询用户地址列表
     */
    @ApiOperation("查询用户地址列表")
    @PreAuthorize("@pms.hasPermission('system:address:list')")
    @GetMapping("/list")
    public TableDataInfo<UserAddress> list(@Validated UserAddress userAddress) {
        return PageUtils.buildDataInfo(userAddressService.page(PageUtils.buildPage()));
    }

    /**
     * 导出用户地址列表
     */
    @ApiOperation("导出用户地址列表")
    @PreAuthorize("@pms.hasPermission('system:address:export')")
    @SysLog("用户地址")
    @GetMapping("/export")
    public List export(@Validated UserAddress userAddress) {
        List<UserAddress> list = userAddressService.list();
        // ExcelUtil<UserAddress> util = new // ExcelUtil<UserAddress>(UserAddress.class);
        return  list;// util.exportExcel(list, "用户地址");
    }

    /**
     * 获取用户地址详细信息
     */
    @ApiOperation("获取用户地址详细信息")
    @PreAuthorize("@pms.hasPermission('system:address:query')")
    @GetMapping("/{id}")
    public R<UserAddress> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserAddress userAddress = userAddressService.getById(id);
        return R.ok(userAddress);
    }

    /**
     * 新增用户地址
     */
    @ApiOperation("新增用户地址")
    @PreAuthorize("@pms.hasPermission('system:address:add')")
    @SysLog("用户地址")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody UserAddress userAddress) {
        return R.okByRow(userAddressService.save(userAddress) ? 1 : 0);
    }

    /**
     * 修改用户地址
     */
    @ApiOperation("修改用户地址")
    @PreAuthorize("@pms.hasPermission('system:address:edit')")
    @SysLog("用户地址")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody UserAddress userAddress) {
        return R.okByRow(userAddressService.updateById(userAddress) ? 1 : 0);
    }

    /**
     * 删除用户地址
     */
    @ApiOperation("删除用户地址")
    @PreAuthorize("@pms.hasPermission('system:address:remove')")
    @SysLog("用户地址")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(userAddressService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
